﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NanoVirtualView
{
    public abstract class BaseVirtualView
    {
        public event MouseEventHandler MouseDown;
        public event MouseEventHandler MouseUp;
        public event MouseEventHandler MouseMove;
        public event EventHandler MouseLeave;
        public event EventHandler MouseEnter;
        public event MouseEventHandler MouseClick;
        public event MouseEventHandler MouseDoubleClick;

        public bool Visible { set; get; } = true;
        public bool Enter = false;
        public PointF Location { set; get; }
        public SizeF Size { get; set; }
        public float Left
        {
            set => Location = new PointF(value, Location.Y);//保留原始Y坐标
            get => Location.X;
        }
        public float Top
        {
            set => Location = new PointF(Location.X, value);//保留原始X坐标
            get => Location.Y;
        }
        public float Width
        {
            set => Size = new SizeF(value, Size.Height);//保留原始高度
            get => Size.Width;
        }
        public float Height
        {
            set => Size = new SizeF(Size.Width, value);//保留原始宽度
            get => Size.Height;
        }
        public RectangleF Rectangle => new RectangleF(Location, Size);
        public RectangleF PaintRectangle => Visible ? Rectangle : RectangleF.Empty;

        public VirtualViewRegister Register { private set; get; }

        public BaseVirtualView(VirtualViewRegister register)
        {
            this.Register = register;
        }
        public void OnMouseDown(MouseEventArgs e)
        {
            MouseDown?.Invoke(this, e);
        }
        public void OnMouseUp(MouseEventArgs e)
        {
            MouseUp?.Invoke(this, e);
        }
        public void OnMouseMove(MouseEventArgs e)
        {
            MouseMove?.Invoke(this, e);
        }
        public void OnMouseLeave(EventArgs e)
        {
            MouseLeave?.Invoke(this, e);
        }
        public void OnMouseEnter(EventArgs e)
        {
            MouseEnter?.Invoke(this, e);
        }
        public void OnMouseClick(MouseEventArgs e)
        {
            MouseClick?.Invoke(this, e);
        }
        public void OnMouseDoubleClick(MouseEventArgs e)
        {
            MouseDoubleClick?.Invoke(this, e);
        }
        public void NotifPaint()
        {
            Register.NotifPaint();
        }
        public void Invalidate(PaintEventArgs e)
        {
            OnPrePaint();
            OnPaint(e);
            OnEndPaint();
        }

        public abstract void OnPaint(PaintEventArgs e);
        public abstract void OnPrePaint();
        public abstract void OnEndPaint();
        public Cursor Cursor { get => Register.Cursor; set => Register.Cursor = value; }
        public void BreakOff()
        {
            this.Register.RemoveVirtualView(this);
        }
    }
}
